﻿@model System.Data.DataTable
@using Wheeler.Domain;
@using Wheeler.Common.DB;
@using Wheeler.Common.Extensions;
@using Wheeler.Web.ViewModels;
@using System.Text;

@{
    String listType = ViewData[AppConstants.ViewDataKeys.LIST_TYPE] as String;
    bool _IsTabularList = false;
    if (String.Compare(listType, "T", true) == 0)
    {
        _IsTabularList = true;
    }
}

@functions{
    public String AddTestResultButton(int vehicleID) {
        return " <a class='btn btn-mini' href='/Private/TestResult/Edit/?VehicleID=" + vehicleID + "'><i class='icon-plus'></i> Enter Data Sheet</a>";
    }
    public String AddPrintButton(int vehicleID) {
        return @" <a class='btn btn-mini' href='/Private/TestResult/Confirm/?VehicleID=" + vehicleID + "'><i class='icon-print'></i> Print Test Result</a>";
    }
    public String AddEditButton(int vehicleID) {
        return "<a class='btn btn-mini' href='/Private/Vehicle/Edit/" + vehicleID + "'><i class='icon-edit'></i> Edit</a>";
    }
    
    public MvcHtmlString GetButtonsHtml(int vehicleID, int testResultCount, int passedTestCount) {
        StringBuilder sb = new StringBuilder(10);
        sb.Append(AddEditButton(vehicleID));
        if(testResultCount > 0) {
            sb.Append(AddPrintButton(vehicleID));
            if(passedTestCount == 0){
                sb.Append(AddTestResultButton(vehicleID));
            }
        }
        else {
            sb.Append(AddTestResultButton(vehicleID));
        }
        return MvcHtmlString.Create(sb.ToString()); 
    }
        
}

@if (Model != null && Model.Rows.Count > 0)
{
    if (_IsTabularList)
    {
        <table class="data-table-list" cellpadding="0" cellspacing="0">
            <tr>
                <th>Registration No</th>
                <th>Chassis No</th>
                <th>Engine No</th>
                <th>Owner Name</th>
                @*<th>Test Status</th>*@
                <th>Action</th>
            </tr>
        @foreach (System.Data.DataRow _Row in Model.Rows)
        {
            int _ID = NullHandler.GetInt(_Row["ID"]);
            //int _TestStatusID = NullHandler.GetInt(_Row["TestStatusID"]);
            //bool _HasPublished = NullHandler.GetBoolean(_Row["HasPublished"]);
            bool _IsVerified = NullHandler.GetBoolean(_Row["IsVerified"]);
            //Wheeler.Domain.DbObject.TestStatus _TestStatus = DbObject.TestStatus.NONE;
            
            ///Added properties after first feedback
            int _TestResultCount = NullHandler.GetInt(_Row["TestResultCount"]);
            int _PassedTestCount = NullHandler.GetInt(_Row["PassedTestCount"]);
            int _MAXTestRound = NullHandler.GetInt(_Row["MAXTestRound"]);
            
            //if (_TestStatusID == 0) {
                //_TestStatus = DbObject.TestStatus.NOT_TESTED;
            //}
            //else {
                //_TestStatus = (DbObject.TestStatus)NullHandler.GetInt(_Row["TestStatus"]);
            //}
            <tr>
                <td>@NullHandler.GetString(_Row["RegistrationNo"])</td>
                <td>@NullHandler.GetString(_Row["ChasisNo"])</td>
                <td>@NullHandler.GetString(_Row["EngineNo"])</td>
                <td>@NullHandler.GetString(_Row["OwnerName"])</td>
                @*<td>@MvcHtmlString.Create(WebUtil.GetTestStatusString((int)_TestStatus))</td>*@
                <td>
                    @GetButtonsHtml(_ID, _TestResultCount, _PassedTestCount)
                </td>            
            </tr>
        }
        </table>
    }
    else
    {
        //Normal Non Tabular Listing
        foreach (System.Data.DataRow _Row in Model.Rows)
        {
            int _ID = NullHandler.GetInt(_Row["ID"]);
            bool _IsVerified = NullHandler.GetBoolean(_Row["IsVerified"]);

            ///Added properties after first feedback
            int _TestResultCount = NullHandler.GetInt(_Row["TestResultCount"]);
            int _PassedTestCount = NullHandler.GetInt(_Row["PassedTestCount"]);
            int _MAXTestRound = NullHandler.GetInt(_Row["MAXTestRound"]);
            <div class="jb-job">
                <div><strong>@NullHandler.GetString(_Row["RegistrationNo"])</strong></div>
                <div class="jb-label-hint">                    
                    Registered On @NullHandler.GetDateTime(_Row["CreatedOn"]).ToString("MMMM dd, yyyy hh:mm:tt")
                </div>
                <div class="para">
                    Chassis No: @NullHandler.GetString(_Row["ChasisNo"])<br />
                    Engine No: @NullHandler.GetString(_Row["EngineNo"])<br />
                    Owner Name: @NullHandler.GetString(_Row["OwnerName"])<br />
                    @*Test Status: @MvcHtmlString.Create(WebUtil.GetTestStatusString((int)_TestStatus))*@
                </div>
                <div>
                    @*@if (_TestStatusID > 0)
                    {
                        if (UserInfo.CanEditVehicleInfo(_HasPublished))
                        {
                            <a class="btn btn-mini" href="/Private/Vehicle/Edit/@_ID"><i class="icon-edit"></i> Edit</a>
                        }
                        <a class="btn btn-mini" href="/Private/TestResult/PrintResult/?VehicleID=@_ID"><i class="icon-print"></i> Print Test Result</a>
                    }
                    else
                    {
                        <a class="btn btn-mini" href="/Private/TestResult/Edit/?VehicleID=@_ID"><i class="icon-plus"></i> Enter Test Result</a>
                    }*@

                    @GetButtonsHtml(_ID, _TestResultCount, _PassedTestCount)
                </div>
            </div>
        }
    }
}